<?php

namespace app\wechat\controller;

use think\Config;
use think\Db;

class OtherGoods extends Common {

    use WechatTrait;

    /**
     * 商品列表
     * @return \think\response\Json
     */
    public function otherGoodsList() {

        $currentCustomer = $this->request->currentCustomer;
        $customerId = $this->request->post("customerId");
        $pageNum = $this->request->post('pageNum');
        $pageSize = $this->request->post('pageSize');

        $customer = $this->customerService->findById($customerId);

        if (isNullOrEmpty($customer)) {
            return errorReturn('errorWechat.errorCustomerNotFound');
        }

        // 定义返回数据
        $returnData['list'] = [];

        $stockList = $this->stockService->getStockListForPage(
            $customerId, $pageNum, $pageSize);

        if (!count($stockList)) {
            return successReturn($returnData);
        }

        // 拼接商品ID字符串
        $goodsIds = '';
        foreach ($stockList as $key=>$val) {
            $goodsIds .= $val['goods_id'].',';
        }
        $goodsIds = substr($goodsIds, 0, strlen($goodsIds)-1);

        $yxGoodsResult = getYxGoodsSearchList(1, count($stockList),
            '', $goodsIds);

        if(!$yxGoodsResult["success"]) {
            $returnError = config("errorWechat.errorYxService");
            $returnError["message"] = $yxGoodsResult["msg"];
            return json($returnError);
        }

        foreach ($stockList as $key=>$val) {

            $info = [];
            foreach ($yxGoodsResult['list']['list'] as $k=>$v) {

                if ($val['goods_id'] == $v['goodsId']) {

                    $info['goods_id'] = $v['goodsId'];
                    $info['goods_name'] = $v['goodsName'];
                    $info['goods_img'] = $v['goodsImg'];
                    $info['favorite_num'] = $v['favoriteNum'];
                    $info['market_price'] = $v['marketPrice'];
                    $info['shop_price'] = $v['shopPrice'];
                    $info['little_desc'] = $val['little_desc'];
                    $info['is_top'] = $val['is_top'];
                }
            }

            $returnData['list'][$key] = $info;
        }

        return successReturn($returnData);

    }

    /**
     * 别人的货架
     * @return \think\response\Json
     */
    public function otherShelf() {

        $currentCustomer = $this->request->currentCustomer;
        $customerId = $this->request->post("customerId");

        $customer = $this->customerService->findById($customerId);

        if (isNullOrEmpty($customer)) {
            return errorReturn('errorWechat.errorCustomerNotFound');
        }

        // 小店推荐商品 小店推荐商品分类ID是 -2
        $recommendGoodsResult = getYxGoodsSearchList(
            1, 10, '-2', '');

        if(!$recommendGoodsResult["success"]) {
            $returnError = config("errorWechat.errorYxService");
            $returnError["msg"] = $recommendGoodsResult["msg"];
            return json($returnError);
        }

        $recommendGoodsList = [];
        foreach ($recommendGoodsResult['list']['list'] as $key=>$val) {

            $recommendGoodsInfo['goods_id'] = $val['goodsId'];
            $recommendGoodsInfo['goods_name'] = $val['goodsName'];
            $recommendGoodsInfo['goods_img'] = $val['goodsImg'];
            $recommendGoodsInfo['favorite_num'] = $val['favoriteNum'];
            $recommendGoodsInfo['market_price'] = $val['marketPrice'];
            $recommendGoodsInfo['shop_price'] = $val['shopPrice'];
            $recommendGoodsInfo['commission'] = $val['commission'];
            $recommendGoodsInfo['little_desc'] = $val['littleDesc'];

            // 判断是否为拼团商品  如果是则显示拼团价格
            if($val['isPintuan'] == 1) {
                $recommendGoodsInfo['shop_price'] = $val['tuanPrice'];
            }

            array_push($recommendGoodsList, $recommendGoodsInfo);
        }

        // 如果我的商品分类json为空
        if (isNullOrEmpty($customer['own_goods_cat_json'])) {

            $customer['own_goods_cat_json'] = $this->calculationMyGoodsCatJson($customer);
        }

        $ownGoodsCatList = json_decode($customer['own_goods_cat_json'], true);
        foreach ($ownGoodsCatList as &$val) {

            $val['goods_list'] = [];

            if ($val['cat_id'] == '-2') {
                $val['goods_list'] = $recommendGoodsList;
            }
        }

        // 已解锁商品数
        $unlockGoodsCount = $this->stockService
            ->countByCustomerId($customer['id']);

        // 在销售中商品数
        $saleGoodsCount = $this->stockService
            ->countValidByCustomer($customer['id']);

        $returnData['info'] = $this->baseCustomerInfo($customer);
        $returnData['unlock_goods_count'] = $unlockGoodsCount;
        $returnData['sale_goods_count'] = $saleGoodsCount;
        $returnData['cat_list'] = $ownGoodsCatList;

        return successReturn($returnData);

    }

    /**
     * 根据分类搜索我的货架
     * @return \think\response\Json
     */
    public function searchOtherShelfByCatId() {

        $currentCustomer = $this->request->currentCustomer;
        $catId = $this->request->post("catId");
        $customerId = $this->request->post("customerId");

        $customer = $this->customerService->findById($customerId);

        if (isNullOrEmpty($customer)) {
            return errorReturn('errorWechat.errorCustomerNotFound');
        }

        // 定义返回数据
        $returnData['list'] = [];

        $stockList = $this->stockService->getSaleStockListByCatId(
            $customer['id'], $catId);

        if (!count($stockList)) {
            return successReturn($returnData);
        }

        // 拼接商品ID字符串
        $goodsIds = '';
        foreach ($stockList as $key=>$val) {
            $goodsIds .= $val['goods_id'].',';
        }
        $goodsIds = substr($goodsIds, 0, strlen($goodsIds)-1);

        $yxGoodsResult = getYxGoodsSearchList(1, count($stockList),
            '', $goodsIds);

        if(!$yxGoodsResult["success"]) {
            $returnError = config("errorWechat.errorYxService");
            $returnError["message"] = $yxGoodsResult["msg"];
            return json($returnError);
        }

        foreach ($stockList as $key=>$val) {

            $info = [];
            foreach ($yxGoodsResult['list']['list'] as $k=>$v) {

                if ($val['goods_id'] == $v['goodsId']) {

                    $info['goods_id'] = $v['goodsId'];
                    $info['goods_name'] = $v['goodsName'];
                    $info['goods_img'] = $v['goodsImg'];
                    $info['favorite_num'] = $v['favoriteNum'];
                    $info['market_price'] = $v['marketPrice'];
                    $info['shop_price'] = $v['shopPrice'];
                    $info['commission'] = $v['commission'];
                    $info['little_desc'] = $val['little_desc'];
                    $info['is_top'] = $val['is_top'];

                    // 判断是否为拼团商品  如果是则显示拼团价格
                    if($v['isPintuan'] == 1) {
                        $info['shop_price'] = $v['tuanPrice'];
                    }

                }
            }

            if (!isNullOrEmpty($info)) {
                $returnData['list'][$key] = $info;
            }
        }

        return successReturn($returnData);

    }

    /**
     * 根据分类搜索我的货架
     * @return \think\response\Json
     */
    public function searchOtherShelfByGoodsName() {

        $currentCustomer = $this->request->currentCustomer;
        $pageNum = $this->request->post("pageNum");
        $pageSize = $this->request->post("pageSize");
        $goodsName = $this->request->post("goodsName");
        $customerId = $this->request->post("customerId");

        $customer = $this->customerService->findById($customerId);

        if (isNullOrEmpty($customer)) {
            return errorReturn('errorWechat.errorCustomerNotFound');
        }

        // 定义返回数据
        $returnData['list'] = [];

        $stockList = $this->stockService->getStockListForPage(
            $customer['id'], $pageNum, $pageSize, $goodsName);

        if (!count($stockList)) {
            return successReturn($returnData);
        }

        // 拼接商品ID字符串
        $goodsIds = '';
        foreach ($stockList as $key=>$val) {
            $goodsIds .= $val['goods_id'].',';
        }
        $goodsIds = substr($goodsIds, 0, strlen($goodsIds)-1);

        $yxGoodsResult = getYxGoodsSearchList(1, count($stockList),
            '', $goodsIds);

        if(!$yxGoodsResult["success"]) {
            $returnError = config("errorWechat.errorYxService");
            $returnError["message"] = $yxGoodsResult["msg"];
            return json($returnError);
        }

        foreach ($stockList as $key=>$val) {

            $info = [];
            foreach ($yxGoodsResult['list']['list'] as $k=>$v) {

                if ($val['goods_id'] == $v['goodsId']) {

                    $info['goods_id'] = $v['goodsId'];
                    $info['goods_name'] = $v['goodsName'];
                    $info['goods_img'] = $v['goodsImg'];
                    $info['favorite_num'] = $v['favoriteNum'];
                    $info['market_price'] = $v['marketPrice'];
                    $info['shop_price'] = $v['shopPrice'];
                    $info['commission'] = $v['commission'];
                    $info['little_desc'] = $val['little_desc'];
                    $info['is_top'] = $val['is_top'];

                    // 判断是否为拼团商品  如果是则显示拼团价格
                    if($v['isPintuan'] == 1) {
                        $info['shop_price'] = $v['tuanPrice'];
                    }

                }
            }

            if (!isNullOrEmpty($info)) {
                $returnData['list'][$key] = $info;
            }
        }

        return successReturn($returnData);

    }

}